<?php

declare(strict_types=1);
/**
 *​
 * WxuserController.php
 * 微信用户管理API
 * User：Abell
 * Date：2020/5/27
 */

namespace App\Controller\Api;

use App\Controller\AbstractController;
use App\Constants\ErrorCode;
use App\Utils\ApiUtils;
use Hyperf\HttpServer\Contract\RequestInterface;
// 业务层
use App\Service\WxuserService;
use Hyperf\Di\Annotation\Inject;
// 验证器
use App\Validate\WxuserValidation;

class WxuserController extends AbstractController
{
    /**
     * @Inject
     * @var WxuserService
     */
    protected $service;

    /** 支付宝小程序保存用户信息
     * @Author abell
     * @DateTime 2020-06-09
     * @return object
     */
    public function saveAliUser(){
        make(WxuserValidation::class)->check($this->request->all(),'logininfo')->response();
        $result = $this->service->saveAliUser($this->request->all());
        return $this->response->json(ApiUtils::send(ErrorCode::SUCCESS,'ok',$result,1));
    }

    /** 获取用户信息  返回获取用户信息的html代码
     * @Author abell
     * @DateTime 2020-06-13
     * @return
     */
    public function getUserInfo(){
        $resulut = $this->service->getUserInfo();
        return $this->response->json($resulut);
    }

    /** 支付宝小程序通过code获取token和user_id
     * @Author abell
     * @DateTime 2020-06-09
     * @return object
     */
    public function getAliToken(){
        make(WxuserValidation::class)->check($this->request->all(),'loginnew')->response();
        $result = $this->service->getToken($this->request->all());
        return $this->response->json($result);
    }

    /** 小程序登录API
     * @Author abell
     * @DateTime 2020-05-26
     * @return object
     */
    public function loginNew(){
        make(WxuserValidation::class)->check($this->request->all(),'loginnew')->response();
        return  $this->jsonApi($this->service->login($this->request->all()));
    }

    /** 小程序授权信息API 编写中,未完成
     * @Author abell
     * @DateTime 2020-05-26
     * @return object
     */
    public function loginInfo(){
        make(WxuserValidation::class)->check($this->request->all(),'logininfo')->response();
        return $this->jsonApi($this->service->loginInfo($this->request->all()));
    }

    /** 小程序签到接口API
     * @Author abell
     * @DateTime 2020-05-26
     * @return object
     */
    public function sign(){
        return $this->jsonApi($this->service->sign());
    }

    /** 小程序签到状态查询API
     * @Author abell
     * @DateTime 2020-05-26
     * @return object
     */
    public function signCheck(){
        return $this->jsonApi($this->service->signCheck());
    }

    /** 小程序页面访问统计接口
     * @Author abell
     * @DateTime 2020-05-26
     * @return object
     */
    public function record(RequestInterface $request)
    {
        make(WxuserValidation::class)->check($request->all(),'record')->response();
        return $this->jsonApi($this->service->record($request->all()));
    }

    /** 小程序分享接口
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $openid 非必须,为空的时候返回token内用户信息
     * @return object
     */
    public function share(RequestInterface $request)
    {
        return  $this->jsonApi($this->service->share($request->all()));
    }

    /** 添加小程序接口
     * @Author abell
     * @DateTime 2020-05-26
     * @return object
     */
    public function addsmall(){
        return $this->jsonApi($this->service->addsmall());
    }
}
